import { Injectable } from '@nestjs/common';
import { UserEntity } from '../entitys/user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
var dayjs = require('dayjs')

@Injectable()
export class UserService {
  constructor (
    @InjectRepository(UserEntity)
    private readonly usersRepository: Repository<UserEntity>,
  ) { }

  // 判断用户是否存在
  async checkUserExistService(userAccount: any) {
    console.log(userAccount)
    const sql = 'SELECT * FROM t_user WHERE f_user_account=?'
    return await this.usersRepository.query(sql, userAccount)
  }

  // 获取用户列表
  async userList(userQuery): Promise<any> {
    let { fName, pageNum, pageSize } = userQuery
    fName = fName ? fName : ''
    const startpage = (pageNum - 1) * pageSize
    const userListsql = `SELECT f_id,f_user_account,f_user_mobile,f_user_email,f_user_photo,f_user_card_id,f_user_real_name,f_user_sex, f_remark, f_state, f_is_super from t_user WHERE f_is_del = "0" AND f_user_account LIKE "%${fName}%"`;
    const countsql = `SELECT COUNT(*) as count from (${userListsql}) tb`
    const limitSql = userListsql + `limit ${startpage},${pageSize}`
    const userList = await this.usersRepository.query(limitSql)
    const count = await this.usersRepository.query(countsql)
    return {
      userList,
      count: count[0].count
    }
  }

  // 新建用户
  async createUser(CreateUserDto): Promise<any> {
    try {
      const sql = 'INSERT INTO `nayxs_go_ny`.`t_user` (`f_id`,`f_user_account`,`f_user_password`,`f_user_real_name`,`f_user_photo`,`f_user_sex`,`f_user_card_id`,`f_user_mobile`,`f_user_call`,`f_user_email`,`f_remark`,`f_create_date`,`f_create_id`,`f_state`,`f_is_del`,`f_is_super`,`f_is_exp`,`f_is_nor` )VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
      const CreateUserDtoArr = [
        CreateUserDto.fId,
        CreateUserDto.fUserAccount,
        CreateUserDto.fUserPassword,
        CreateUserDto.fUserRealName,
        CreateUserDto.fUserPhoto,
        CreateUserDto.fUuserSex,
        CreateUserDto.fUserCardId,
        CreateUserDto.fUserMobile,
        CreateUserDto.fUserCall,
        CreateUserDto.fUserEmail,
        CreateUserDto.fRemark,
        dayjs(CreateUserDto.fCreateDate).format('YYYY-MM-DD'),
        CreateUserDto.fCreateId,
        CreateUserDto.fState,
        CreateUserDto.fIsDel,
        CreateUserDto.fIsSuper,
        CreateUserDto.fIsExp,
        CreateUserDto.fIsNor
      ]
      console.log(CreateUserDtoArr)
      return await this.usersRepository.query(sql, CreateUserDtoArr)
    } catch (error) {
      console.error(error);
    }
  }

  // 获取用户详情
  async getUserById(userId) {
    const sql = 'SELECT * from t_user where f_id=?'
    return await this.usersRepository.query(sql, userId)
  }

  // 修改用户信息
  async updateUserById(CreateUserDto) {
    return '修改用户信息';
  }

  // 删除用户信息
  async deleteUserById(userId) {
    const sql = "UPDATE t_user SET f_is_del = '1' WHERE f_id =?"
    return await this.usersRepository.query(sql, userId.f_id)
  }
}
